﻿Imports System.ComponentModel

Namespace Extensions

    Public Module EnumExtensions

        <System.Runtime.CompilerServices.Extension()> _
        Public Function GetDescription(ByVal element As [Enum]) As String

            Dim type = element.GetType()
            Dim memberInfo = type.GetMember(element.ToString())

            If memberInfo IsNot Nothing And memberInfo.Length > 0 Then

                Dim attributes() = memberInfo(0).GetCustomAttributes(GetType(DescriptionAttribute), False)

                If attributes IsNot Nothing And attributes.Length > 0 Then
                    Return CType(attributes(0), DescriptionAttribute).Description
                End If

            End If

            Return element.ToString()

        End Function

    End Module

End Namespace
